package com.ruiyian.project.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiyian.common.core.domain.entity.Point;
import com.ruiyian.project.equipment.domain.FireFillWater;
import com.ruiyian.project.equipment.mapper.FireFillWaterMapper;
import com.ruiyian.project.equipment.service.FireFillWaterService;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/7/18
 * Desc: 消防补水信息
 */
@Service
public class FireFillWaterServiceImpl extends ServiceImpl<FireFillWaterMapper, FireFillWater> implements FireFillWaterService {

    /**
     * 批量保存
     * @param list
     */
    @Override
    public void saveBatch(List<Point> list) {
        List<FireFillWater> data = new ArrayList<>();
//        System.out.println("补水数据"+JSONArray.fromObject(list).toString());
        Map<Long, List<Point>> collect = list.stream().collect(Collectors.groupingBy(p -> p.getTunnelId()));
        for(Long tunnelId: collect.keySet()){
            FireFillWater fillWater = new FireFillWater();
            fillWater.setTunnelId(tunnelId);
            list.stream().forEach(p -> {
                String value = p.getValue();
                switch (p.getCode()){
                    case "signal_1":
                        fillWater.setAreaHigh(value);
                        break;
                    case "signal_2":
                        fillWater.setAreaLow(value);
                        break;
                    case "y":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("yyyy","20"+value));
                        break;
                    case "M":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("MM",value));
                        break;
                    case "d":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("dd",value));
                        break;
                    case "h":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("hh",value));
                        break;
                    case "m":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("mm",value));
                        break;
                    case "s":
                        fillWater.setStartTime(fillWater.getStartTime().replaceAll("ss",value));
                        break;
                    case "y_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("yyyy","20"+value));
                        break;
                    case "M_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("MM",value));
                        break;
                    case "d_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("dd",value));
                        break;
                    case "h_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("hh",value));
                        break;
                    case "m_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("mm",value));
                        break;
                    case "s_e":
                        fillWater.setEndTime(fillWater.getEndTime().replaceAll("ss",value));
                        break;
                    case "signal_3":
                        fillWater.setFillwaterModel(value);
                        break;
                    case "signal_4":
                        fillWater.setCabinetModel(value);
                        break;
                    case "loop_1":
                        fillWater.setStartHigh(value);
                        break;
                    case "loop_2":
                        fillWater.setStartLow(value);
                        break;
                    case "loop_3":
                        fillWater.setStopHigh(value);
                        break;
                    case "loop_4":
                        fillWater.setStopLow(value);
                        break;

                    case "1_s_h":
                        fillWater.setRunTime(fillWater.getRunTime().replaceAll("h",value));
                        break;
                    case "1_s_m":
                        fillWater.setRunTime(fillWater.getRunTime().replaceAll("m",value));
                        break;
                    case "1_s_s":
                        fillWater.setRunTime(fillWater.getRunTime().replaceAll("s",value));
                        break;
                }


            });
            data.add(fillWater);
        }
        if(data.size()>0){
            data.stream().forEach(f->{
                List<FireFillWater> listFire = this.getList(f);
                if(listFire.size()==0){
                    this.save(f);
                }
            });
        }
//        this.saveBatch(data);
    }


    @Override
    public List<FireFillWater> getList(FireFillWater fireFillWater) {
        QueryWrapper<FireFillWater> queryWrapper = new QueryWrapper<>();
        if(null!=fireFillWater.getTunnelId()){
            queryWrapper.eq("tunnel_id",fireFillWater.getTunnelId());
        }
        if(StringUtils.isNotBlank(fireFillWater.getStartTime())){
            queryWrapper.eq("start_time",fireFillWater.getStartTime());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据隧道id获取月份统计次数
     * @param tunnelId
     * @return
     */
    @Override
    public Map<String,Object> selectCountList(Long tunnelId) {
        Map<String,Object> returnData = new HashMap<>();
        List<String> category = new ArrayList<>();
        List<Map<String,Object>> series = new ArrayList<>();
        List<Object> value = new ArrayList<>();
        List<Map<String, Object>> list = this.baseMapper.selectCountList(tunnelId);
        if(list.size()>0){
            for(Map<String, Object> p: list){
                if(null==p.get("time")){
                    continue;
                }
                category.add(p.get("time").toString());
                value.add(p.get("value"));
            }
        }
        Map<String,Object> data = new HashMap<>();
        data.put("data",value);
        data.put("name","执行次数");
        series.add(data);
        returnData.put("category",category);
        returnData.put("series",series);
        return returnData;
    }

    @Override
    public List<FireFillWater> selectListPage(FireFillWater fireFillWater) {
        return this.baseMapper.selectListPage(fireFillWater);
    }
}
